<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>home</title>
	<link rel="stylesheet" type="text/css" href="${ctx}/themes/gray/easyui.css">
	<script type="text/javascript" src="${ctx}/script/jquery.min.js"></script>
	<script type="text/javascript" src="${ctx}/script/jquery.easyui.min.js"></script>
	
	<link rel="stylesheet" type="text/css" href="${ctx}/script/ztree/jquery.ztree.css">
	<script type="text/javascript" src="${ctx}/script/ztree/jquery.ztree.min.js"></script>
	
	<link rel="stylesheet" type="text/css" href="${ctx}/script/jqgrid/css/jquery-ui-1.8.2.custom.css">
	<link rel="stylesheet" type="text/css" href="${ctx}/script/jqgrid/css/ui.jqgrid.css">
	<script type="text/javascript" src="${ctx}/script/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="${ctx}/script/jqgrid/js/jquery.jqGrid.min.js"></script>
	
	<script type="text/javascript" src="${ctx}/script/lhgdialog/lhgdialog.min.js?s=facebook"></script>
	<script>
	var zTree;
	var setting = {
		open:true,
		isSimpleData : true,
   		treeNodeKey : "id",
   		treeNodeParentKey : "pId",
		async: true,
		asyncUrl: "showZzjgTree.action",  //获取节点数据的URL地址
		asyncParam: ["id", "name"], //获取节点数据时，必须的数据名称，例如：id、name
		callback : {
	      click: zTreeOnClick,
	      rightClick: zTreeOnRightClick,
	      asyncSuccess:zTreeOnAsyncSuccess
	    }
	};
	var zNodes =[];
	
	$(document).ready(function(){
		zTree = $("#tree").zTree(setting, zNodes);
		
		//绑定右键菜单事件
		//同级添加
		$('#mm-tjtj').click(function(){
			var srcNode = zTree.getSelectedNode();
			//alert(srcNode.pId);
			if (srcNode && srcNode.pId != null) {
				new jQuery.dialog({id:'1',title: '新增组织机构',page:'addorModZzjg.action?sjjgid='+srcNode.pId,width:400,height:200,rang:true,iconTitle:false}).ShowDialog();
			} else {
				alert("根节点不能同级添加！");
			}	
		})
		//下级添加
		$('#mm-xjtj').click(function(){
			var srcNode = zTree.getSelectedNode();
			if (srcNode) {
				new jQuery.dialog({id:'1',title: '新增组织机构',page:'addorModZzjg.action?sjjgid='+srcNode.id,width:400,height:200,rang:true,iconTitle:false}).ShowDialog();
			} else {
				alert("选择错误！");
			}	
		})
		//修改节点
		$('#mm-xgjd').click(function(){
			var srcNode = zTree.getSelectedNode();
			if (srcNode) {
				new jQuery.dialog({id:'1',title: '修改组织机构',page:'addorModZzjg.action?jgbh='+srcNode.id+'&sjjgid='+srcNode.pId,width:400,height:200,rang:true,iconTitle:false}).ShowDialog();
			} else {
				alert("选择错误！");
			}
		});
		//删除节点
		$('#mm-scjd').click(function(){
			actionOnDelZzjg();
		});
		
		/*
		$("#jgmc").blur(function() {
			updateTreeNode($("#jgbh").val(),this.value);
		});
		*/
	});
		
    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    	zTree.expandAll(true);
	}
    
    		
	function zTreeOnClick(event, treeId, treeNode) {
		var URL = 'listJgry.action?jgbh='+treeNode.id;
		$("#jgbh").val(treeNode.id);
		//$("#jgmc").val(treeNode.name);
		$("#gridTable").setGridParam({url:URL}).trigger("reloadGrid"); 
	}
	
	function zTreeOnRightClick(event, treeId, treeNode) {
		zTree.selectNode(treeNode);
		$('#treeMenu').menu('show', {left: event.clientX, top: event.clientY});
	}
	
	//删除组织结构
	var actionOnDelZzjg = function() {
		if(confirm("您确定要删除该机构吗？")) {
			var srcNode = zTree.getSelectedNode();
			if (srcNode && srcNode.nodes && srcNode.nodes.length > 0) {
				alert("该机构下有子机构，请先删除子机构后再删除！");
			} else {
				var params = {
					"jgbh" : srcNode.id,
					"sjjg" : srcNode.pId
				};
				$.ajax({
					url : "getJgryCount",
					data : params,
					dataType : "json",
					cache : false,
					error : function(textStatus, errorThrown) {
						alert("系统ajax交互错误: " + textStatus);
					},
					success : function(data, textStatus) {
						if (data.jgryCount > 0) {
							if(confirm("该机构下有机构人员，您确定要删除该机构并删除该机构下才人员吗？")) {
								ajaxDelZzjg(params);
							}
						} else {
							ajaxDelZzjg(params);
						}
					}
				});
			}
			return true;
		} else {
			return false;
		}
	};
	
	function ajaxDelZzjg(params) {
		$.ajax({
			url : "deleteGG_JGBH.action",
			data : params,
			dataType : "json",
			cache : false,
			error : function(textStatus, errorThrown) {
				alert("系统ajax交互错误: " + textStatus);
			},
			success : function(data, textStatus) {
				if (data.ajaxResult == "success") {
					//删除成功默认选择一级节点，并刷新列表
					var treeNode = zTree.getNodeByParam("id", params.sjjg);
					zTree.selectNode(treeNode);
					var URL = 'listJgry.action?jgbh='+params.sjjg;
					$("#jgbh").val(params.sjjg);
					$("#gridTable").setGridParam({url:URL}).trigger("reloadGrid"); 
		
					removeTreeNode(params.jgbh);
					alert("删除成功!");
				} else {
					alert("删除操作失败!");
				}
			}
		});
	}
	
	function addTreeNode(fid,newNode) {
		var srcNode = zTree.getSelectedNode();
		if (srcNode && srcNode.id == fid) {
			zTree.addNodes(srcNode, newNode);
		} else {
			srcNode = zTree.getNodeByParam("id", fid);
			zTree.addNodes(srcNode, newNode);
		}
	}
	
	function removeTreeNode(id) {
		var srcNode = zTree.getNodeByParam("id", id);
		if (srcNode) {
			if (srcNode.nodes && srcNode.nodes.length > 0) {
				var msg = "要删除的节点是父节点，如果删除将连同子节点一起删掉。\n\n请确认！";
				if (confirm(msg)==true){
					zTree.removeNode(srcNode);
				}
			} else {
				zTree.removeNode(srcNode);
			}
		}	
	}
	
	function updateTreeNode(id,name) {
		var srcNode = zTree.getNodeByParam("id", id);
		srcNode.name = name;
		zTree.updateNode(srcNode);
	}
	
	$(function(){
		$("#fid").val("ROOT");
		jQuery("#gridTable").jqGrid({
		   	url:'listJgry.action',
			datatype: 'json',
		   	colNames:['ID','姓名', '登录号','操作'],
		   	colModel:[
		   		{name:'ID', index:'ID',width:150,fixed:true,align:'center',sortable:false,hidden:true},
		   		{name:'MC', index:'MC',width:200,align:'center',sortable:false},
		   		{name:"DLH",index:'DLH',width:200,align:'center',sortable:false},
			    {name:'CZ',index:'ID',width:90,fixed:true,sortable:false, resize:false,formatter:cleverActions,formatoptions:{keys:true}}
		   	],
		   	rowNum: 20,
		   	autowidth: true,
			rowList: [1,2,3,4,5,6,7,8,9,10,15,20,30,40,50,100],
			prmNames: {search: "search"},
			jsonReader: {
				root:"gridModel",
				records: "record",
				repeatitems : false
			},
		   	pager: '#gridpager',
		    viewrecords: true,
		    sortname: 'id',
		    //rownumbers: true,
		    multiselect: true,
		    multiboxonly: true,
		    altRows:true,//隔行变色
		    altclass:'altclass',
		    caption:"机构人员信息"
		});
		$("#gridTable").jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false,search:false,refresh:false});
		
		$("#gridTable").jqGrid().navButtonAdd('#gridpager',{
		   caption:"", 
		   title:"添加新记录",
		   buttonicon:"ui-icon-plus", 
		   onClickButton: function(){ 
		   	   actionOnAdd();
		   }
		});
		
		$("#gridTable").jqGrid().navButtonAdd('#gridpager',{
		   caption:"", 
		   title:"删除所选记录",
		   buttonicon:"ui-icon-trash", 
		   onClickButton: function(){ 
	       	   actionOnDel();
		   }
		}).navSeparatorAdd("#gridpager",{position:"first"});
		doResize();
	});
	
	//设置jqGrid高度
	function doResize() {
		jQuery("#gridTable").jqGrid('setGridHeight',$(document).height()-110);
	}
	
	//行formatter
	function cleverActions(cellvalue, options, rowObject) {
	    return '<div id="' + options.rowId + '" class="ui-corner-all clever-jqgrid-action clever-jqgrid-edit" style="float:left;margin-left:10px;padding:0px;" title="' + $.jgrid.nav.edittitle + '" onclick="actionOnEditLine(this,event);" onmouseover="actionMouseover(this,event)" onmouseout="actionMouseout(this,event)" ><span class="ui-icon ui-icon-pencil"></span></div>'
	    	 + '<div id="' + options.rowId + '" class="ui-corner-all clever-jqgrid-action clever-jqgrid-del"  style="float:left;margin-left:10px;padding:0px;" title="' + $.jgrid.nav.deltitle  + '" onclick="return actionOnDelLine(this,event);" onmouseover="actionMouseover(this,event)" onmouseout="actionMouseout(this,event)" ><span class="ui-icon ui-icon-trash"></span></div>'
	    	 + '<div id="' + options.rowId + '" class="ui-corner-all clever-jqgrid-action clever-jqgrid-gear" style="float:left;margin-left:10px;padding:0px;" title="功能授权" onclick="actionOnGnsqLine(this,event);" onmouseover="actionMouseover(this,event)" onmouseout="actionMouseout(this,event)" ><span class="ui-icon ui-icon-gear"></span></div>';
	}
	
	//行formatter鼠标移入样式
	function actionMouseover(el, ev) {
	    $(el).addClass('ui-state-hover');
	    /*
	    if ($.browser.msie) {
	        ev.cancelBubble = true;
	    } else {
	        ev.stopPropagation();
	    }*/
	}
	
	//行formatter鼠标移出样式
	function actionMouseout(el, ev) {
	    $(el).removeClass('ui-state-hover');
	    if ($.browser.msie) {
	        ev.cancelBubble = true;
	    } else {
	        ev.stopPropagation();
	    }
	}
	
	//行操作-修改人员
	var actionOnEditLine = function(el, ev) {  
		var rowId = $(el).attr('id');
	    var rowData = $("#gridTable").getRowData(rowId);
		new jQuery.dialog({id:'1',title: '修改人员信息',page:'addorModJgry.action?id='+rowData.ID,width:400,height:200,rang:true,iconTitle:false}).ShowDialog();
	};	

	//行操作-删除人员
	var actionOnDelLine = function(el, ev) {
		if(confirm("您确定要删除该记录吗？")) {
			var rowId = $(el).attr('id');
		    var rowData = $("#gridTable").getRowData(rowId);
		    var srcNode = zTree.getNodeByParam("id", rowData.ID);
			var params = {
				"id" : rowData.ID
			};
			var actionUrl = "deleteJgry.action";
			$.ajax({
				url : actionUrl,
				data : params,
				dataType : "json",
				cache : false,
				error : function(textStatus, errorThrown) {
					alert("系统ajax交互错误: " + textStatus);
				},
				success : function(data, textStatus) {
					if (data.ajaxResult == "success") {
						$("#gridTable").delRowData(rowId);
						removeTreeNode(rowData.ID);
						alert("删除成功!");
					} else {
						alert("删除操作失败!");
					}
				}
			});
			return true;
		} else {
			return false;
		}
	};
	
	//行操作-功能授权
	var actionOnGnsqLine = function(el, ev) {
		var jgbh = $("#jgbh").val();
		var rowId = $(el).attr('id');
	    var rowData = $("#gridTable").getRowData(rowId);
	    new jQuery.dialog({id:'1',title: '功能授权',page:'jgryGnsq.action?jgbh='+jgbh+'&czyid='+rowData.ID,width:368,height:505,rang:true,iconTitle:false}).ShowDialog();
	};	
	
	//新增人员
	var actionOnAdd = function() {  
		var jgbh = $("#jgbh").val();
		//alert(jgbh);
		new jQuery.dialog({id:'1',title: '新增人员',page:'addorModJgry.action?jgbh='+jgbh,width:400,height:200,rang:true,iconTitle:false}).ShowDialog();
	};  
	
	//删除人员
	var actionOnDel = function() {
		var selr = jQuery('#gridTable').jqGrid('getGridParam','selarrrow');
		if(selr.length) {
			if(confirm("您确定要删除所选记录吗？")) {
				var ids = "";
				for(var i = 0; i < selr.length; i++) {
					ids += $('#gridTable').jqGrid('getCell',selr[i],'ID') + ",";
				}
				ids = ids == "" ? ids : ids.substring(0,ids.length-1);
				var params = {
					"id" : ids
				};
				var actionUrl = "deleteJgry.action";
				$.ajax({
					url : actionUrl,
					data : params,
					dataType : "json",
					cache : false,
					error : function(textStatus, errorThrown) {
						alert("系统ajax交互错误: " + textStatus);
					},
					success : function(data, textStatus) {
						if (data.ajaxResult == "success") {
							for(; 0 < selr.length ;) {
								$("#gridTable").delRowData(selr[0]);
							}
							alert("删除成功！");
						} else {
							alert("删除失败！");
						}
					}
				});
				return true;
			}
		} else {
			alert("至少选择一条记录！");
			return false;
		}
	};
	
	var actionOnSearch = function() {
		new jQuery.dialog({id:'1',title: '查询条件',page:'view/xt/jgqx/gnsq/QueryGG_CZLB.jsp',width:380,height:180,rang:true,iconTitle:false}).ShowDialog();
	};
	</script>
</head>
<body class="easyui-layout">
	<div region="west" border="true" split="true" title="" style="width:200px;padding:0px;border-left:1px solid #99BBE8;">
	<ul id="tree" class="tree"></ul>	
	</div>
	<div id="treeMenu" class="easyui-menu" style="width:100px;">
		<div id="mm-tjtj">同级添加</div>	
		<div class="menu-sep"></div>
		<div id="mm-xjtj">下级添加</div>	
		<div class="menu-sep"></div>
		<div id="mm-xgjd">修改节点</div>
		<div class="menu-sep"></div>
		<div id="mm-scjd">删除节点</div>
	</div>

	<div id="mainPanle" region="center" border="false">
	<!-- 
		<table title="机构信息" iconCls="icon-title">
			<tbody>
			<tr>
				<td>机构编号:</td>
				<td><input id="jgbh"  name="jgbh" style="width:150px;" value=""></td>
				<td>&nbsp;&nbsp;&nbsp;机构名称:</td>
				<td><input id="jgmc" name="jgmc"  style="width:150px;" value=""></td>
			</tr>
			</tbody>
		</table>
	 -->	
		<input type="hidden" id ="jgbh" name="jgbh" value="">
		<table id="gridTable"></table>
		<div id="gridpager"></div>    	
	</div>
</body>
</html>